<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Authorize 
{

	function __construct()
	{
		$this->CI =& get_instance();
	}

	function is_logged_in()
	{
		if ($this->CI->session) {
			if ($this->CI->session->userdata('logged_in')) {
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
	function is_admin_logged_in()
	{
		if ($this->CI->session) {
			if ($this->CI->session->userdata('admin_logged_in')) {
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}
	/**
	 * 当前用户id
	 */
	function current_user() {
		return $this->CI->session->userdata('user_id');
	}
	/**
	 * 当前管理员
	 */
	function current_admin_user() {
		return $this->CI->session->userdata('admin_user_id');
	}
	function login_routine()
	{
		$this->CI->load->library('form_validation');
		
		$this->CI->form_validation->set_rules('user_id', '邮箱/用户名', 'required');
		$this->CI->form_validation->set_rules('password', '密码', 'required');
		if ($this->CI->form_validation->run() == FALSE)
		{
			return 3;
		}
		$password = $this->CI->input->post('password');
		$username = $this->CI->input->post('user_id');
		$remember = $this->CI->input->post('remember_password');

		$query = $this->CI->db->query("select id, nickname, password,status from users where nickname=" . $this->CI->db->escape($username) .
		" or email=" . $this->CI->db->escape($username));
		$login_result = 0;
		if ($query->num_rows() > 0) {
			$rows = $query->result_array();
			foreach ($rows as $row ) {
				$pass_array = split(":", $row['password']);
				if(md5( $password . $pass_array[1]) == $pass_array[0]) {
					$login_result = 1;
					$id = $row['id'];
					$nickname = $row['nickname'];
					$status = $row['status'];
					break;
				}
			}
		} else { //用户名不存在
			return '2';
		}
		$query->free_result();
		if ($login_result == 1) {
			if ($status == 0) {
				return 4;//用户未激活
			} else if ($status == -1) {
				return 6;//用户被注销
			} else if ($status == -3) {
				return 5;
			}
//			$sql = "select count(*) as total from banned where user_id=" . $id . " and type=-2";
//			$_result = $this->CI->db->query($sql);
//			$_row = $_result->row_array();
//			
//			if($_row['total'] > 0) {
//				return 5;//禁止用户
//			}
			$credentials = array('user_id' => $id, 'logged_in' => $login_result, 'nickname' => $nickname);
			$this->CI->session->sess_destroy();
			$this->CI->session->set_userdata($credentials);
			if ($remember) {
				$cookie = $cookie = array(
				    'name'   => 'nickname',
				    'value'  => $nickname,
				    'expire' => '86500'
				);
				$this->CI->input->set_cookie($cookie); 
				$cookie = $cookie = array(
				    'name'   => 'pass',
				    'value'  => $password,
				    'expire' => '86500'
				);
				$this->CI->input->set_cookie($cookie); 
			}
			return '1';//登录成功
		} else {
			return '0';
			//redirect('user/login_fail/');
		}
	}
	
	function logout() {
		$this->CI->session->sess_destroy();
	}
	/**
	 * 管理员登录验证
	 */
	function admin_login() {
		$password = $this->CI->input->post('password');
		$nickname = $this->CI->input->post('nickname');
		$query = $this->CI->db->query("select id, nickname, password from users where is_admin=1 and nickname=" . $this->CI->db->escape($nickname));
		$login_result = 0;
		if ($query->num_rows() > 0) {
			$row = $query->row_array();
			$pass_array = split(":", $row['password']);
			if(md5( $password . $pass_array[1]) == $pass_array[0]) {
				$login_result = 1;
				$credentials = array('admin_user_id' => $row['id'], 'admin_logged_in' => $login_result, 'admin_nickname' => $nickname);
				$this->CI->session->set_userdata($credentials);
				return 1;
			}
	
		} else { //用户名不存在
			return '2';
		}
		$query->free_result();
	}
}
?>
